package br.pucrio.inf.lac.konoha.hyuga.events.csv;

import java.io.Serializable;

import br.pucrio.inf.lac.konoha.hyuga.events.LocationUpdateEvent;

/**
 * Represent the basic location update event. This event is adapted for CSV stream files.
 * It contains the mobile nodes position (latitude, longitude), and ID. This event is a pre-event, that is,
 * it will be enriched to become the entry point event LocationUpdate.
 * 
 * @author Marcos Roriz <mroriz@inf.puc-rio.br>
 */
public class CSVLocationUpdateEvent implements Comparable<CSVLocationUpdateEvent>, Serializable {
    private String uuid;
	private double latitude;
	private double longitude;
	private long   timestamp;

	// Default Empty Constructor
	public CSVLocationUpdateEvent() {
	}
	
    // Default Empty Constructor
    public CSVLocationUpdateEvent(String uuid, double latitude, double longitude) {
        this(uuid, latitude, longitude, 0);
    }

    // Full Constructor
    public CSVLocationUpdateEvent(String uuid, double latitude, double longitude, long timestamp) {
        this.uuid      = uuid;
        this.latitude  = latitude;
        this.longitude = longitude;
        this.timestamp = timestamp;
    }
    
	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public String getUuid() {
		return uuid;
	}
	
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

    public long getTimestamp() {
        return timestamp;
    }
    
    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public LocationUpdateEvent toLU() {
        return new LocationUpdateEvent(latitude, longitude);
    }
    
    public String toCSVLog() {
        StringBuffer sb = new StringBuffer();
        sb.append(uuid).append(",");
        sb.append(latitude).append(",");
        sb.append(longitude).append(",");
        sb.append(timestamp);
        
        return sb.toString();
    }
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("uuid: ").append(uuid).append(", ");
		sb.append("latitude: ").append(latitude).append(", ");
		sb.append("longitude: ").append(longitude);
		
		return sb.toString();
	}

    @Override
    public int compareTo(CSVLocationUpdateEvent otherPoint) {
        try {
            double uuidnumber      = Double.parseDouble(uuid);
            double otheruuidnumber = Double.parseDouble(otherPoint.getUuid());
            
            return (int) (uuidnumber - otheruuidnumber);
        } catch (NumberFormatException ex) {
            return uuid.compareToIgnoreCase(otherPoint.getUuid());
        }
    }


}
